//
// Created by sai on 2019/4/20.
//
#include "bucketsort.h"

bool isLast(Position node) {
    return node->next == NULL;
}

bool isEmpty(List list) {
    return list->next == NULL;
}

List makeEmpty() {
    List head;
    head = malloc(sizeof(struct Node));
    head->next = NULL;
    return head;
}

Position find(ElemType data, List list) {
    Position pos;
    pos = list->next;
    while (pos != NULL && pos->data != data)
        pos = pos->next;
    return pos;
}

Position findPrevious(ElemType data, List list) {
    Position pos;
    pos = list;
    while (pos->next != NULL && pos->next->data != data)
        pos = pos->next;
    return pos;
}

bool InsertNode(ElemType data, List list, Position pos) {
    Position TmpPos;
    TmpPos = malloc(sizeof(struct Node));
    if (TmpPos == NULL)
        return false;
    TmpPos->data = data;
    TmpPos->next = pos->next;
    pos->next = TmpPos;
    return true;
}

bool DeleteNode(ElemType data, List list) {
    Position pos, NextPos;
    pos = findPrevious(data, list);
    if (pos == NULL)
        return false;
    NextPos = pos->next;
    pos->next = NextPos->next;
    free(NextPos);
    return true;
}

void DeleteList(List list) {
    Position pos, TmpPos;
    pos = list->next;
    list->next = NULL;
    while (pos != NULL) {
        TmpPos = pos->next;
        free(pos);
        pos = TmpPos;
    }
    free(list);
}

void appendNode(ElemType data, List list) {
    Position pos;
    pos = list;
    while (pos->next != NULL)
        pos = pos->next;
    InsertNode(data, list, pos);
}

void PrintBuckets(List list) {
    Position pos;
    pos = list->next;
    while (pos != NULL) {
        printf("%d\n", pos->data);
        pos = pos->next;
    }
}

void InitialBuckets(Bucket buckets, int size) {
    for (int i = 0; i < size; i++)
        buckets[i] = NULL;
}

void GenerateList(List list, int size) {
    int element;
    srand(time(0));
    for (int i = 0; i < size; i++) {
        element=rand()%1000+1;
        appendNode(element,list);
    }
}